﻿using System.Data.Entity;

namespace Domain
{
    public class NorthwindDbContext:DbContext
    {
        public NorthwindDbContext():base("northwind")
        {
            
        }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Purchase> Purchases { get; set; }
        public DbSet<PurchaseItem> PurchaseItems { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<MediaArticle> MediaArticles { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Purchase>()
                .HasKey(p=>p.ID)
                .HasOptional(p=>p.Customer)
                .WithMany(c=>c.Purchases)
                .HasForeignKey(p=>p.CustomerID)
                .WillCascadeOnDelete(true);
            modelBuilder.Entity<PurchaseItem>()
                .HasKey(pi=>pi.ID)
                .HasRequired(pi=>pi.Purchase)
                .WithMany(p=>p.PurchaseItems)
                .HasForeignKey(pi=>pi.PurchaseID)
                .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);
        }
    }
}